package model.playingfield;

import java.awt.Color;
import java.util.ArrayList;
import java.util.HashMap;

import model.Player;

/**
 * This is the PlayingField. It consists of three areas, each
 * made up of spots: the starting areas, the circuit, and the runways.
 * 
 * It has 4 Starting areas, each consisting of 4 Spots. This
 * is where each players Pieces will be placed at the onset 
 * of the game.
 * 
 * The main Circuit is a set of 40 Spots, leading around in a 
 * connected pattern. 
 * 		* 4 of these Spots are starting points, onto which each player
 * 		  moves his/her Pieces from the starting area to the circuit.
 * 		* 4 of these Spots are entry points to each players Runway.
 * 
 * A players Runway is a set of 4 spots, leading up into the Nest.
 * 
 * The Nest is merely an area out of which Pieces cannot be moved,
 * and needs no logical representation.
 * 
 * Further, there are logical connections between all these three 
 * classes of 
 */
public class PlayingField {
	
	// Player pieces and their locations	
	private HashMap<Player, Integer[]> pieceIndex = new HashMap<Player, Integer[]>();
			
	/*
	 * Index over all spots:
	 *   0 - 39  are the spots on the main circuit
	 *   40 - 56 are the spots leading to the nest from a Spot
	 *   57 - 73 are the starting areas
	 */
	private ArrayList<Spot> spotIndex = new ArrayList<Spot>();
	
	// Index over where each players pieces are at the moment
	
	public PlayingField(HashMap<Integer, Player> players) {
		
		// LESS IS MORE! I am going to kill this incredibly bloated model
		// and only reinstate it if we REALLY need buffer code. Good riddance!
		// What a waste of time.
		
		
		/*
		 * Initiate the circuit:
		 * 	 first initiate the Spots 	   	    (spots 0, 10, 20, 30)
		 *   second initiate entry spots on the circuit (spots 1, 11, 21, 31)
		 *   third initiate the Spots running up to the nest for each player
		 *   fourth initiate the starting area Spots   
		 *   last initiate all other Spots, and tie them together
		 */
		
		// Initiate all spots
		for(int i = 0; i < 72; i++)
			spotIndex.add(i, new Spot(Color.WHITE, i));
		
		// Initiate branching Spots and starting Spots
		for(int i = 0, player = 0; i < 40; i += 10, player++) {
			spotIndex.get(i).setOwner(players.get(player));     // Branching Spots  
			spotIndex.get(i+1).setOwner(players.get(player));   // Entry Spots			
		}
		
		// Initiate Spots running up to the nest for each player, as well as starting Spots.
		for(int i = 40, player = 0; i < 57;) {
			for(int k = 1; k < 4; k++) {
				spotIndex.get(i).setOwner(players.get(player));      // Entry Spots
				spotIndex.get(i+17).setOwner(players.get(player));   // Starting Spots
				i++;
			}
			player++;
		}
		
		/*
		 * Initiate the pieceIndex, in order to keep track of where each
		 * players pieces are. All pieces start in their players starting 
		 * area.
		 */
		int counter = 57;
		for(int i = 0; i < 4; i++) {
			Player player = players.get(i);
			pieceIndex.put(player, new Integer[4]);
			
			for(int j = 0; j < 4; j++)
				pieceIndex.get(player)[i] = counter++;		
		}
	}
		
	public void moveToStart(Spot spot){};
	
	/**
	 * Move a given players piece from its current location to a new one.
	 * 
	 * @param player the player whose piece to move
	 * @param index  index at which the piece resides
	 * @param moves  how many moves should be made
	 * @return the new index of the piece
	 */
	public int movePieceAt(Player player, int index, int moves) {
	
		Piece occupant = spotIndex.get(index).getOccupant();
	
		if(occupant == null)
			//some error handling...
			return -1;
		else if(occupant.getOwner() != player) {
			// Some other handling...
			return -1;
		}
		else {
			spotIndex.get(index).removeOccupant();
			// Very primitive, should be interpreted in accordance with rules,
			// This should be checked upstream
			spotIndex.get(index + moves).setOccupant(occupant);

			return (index + moves);
		}
	}
	
	public HashMap<Player, Integer[]> getPieceLocations() {
		return pieceIndex;
	}
} // PlayingField()
		
		
/*
 * // Initiate starting area Spots (Spots 57-73)
		for(int i = 1, j = 39, k = -1; i < 40; i++) {
			
		}
		
		Spot temp = new Spot(null, Color.white, 0);
		spotIndex.add(0, temp);
		
		for(int i = 0, j = 39, k = -1; i < 40; i++) {
	
			// Every 10th Spot is a Spot
			if(i % 9 == 0) {
				Player player = players[k++];
				Spot branch = new Spot(
						null, 
						new Spot(null, player, j++), 
						player,
						i);
	
				temp.setNext(branch);
				temp = temp.getNext();
				
				// Initiate the four spots on the runway to the nest
				Spot temp2 = (Spot)branch.getBranch();
				for(int l = 0; l < 4; l++) {
					temp2.setNext(new Spot(null, player, j++));
					temp2 = (Spot)temp2.getNext();
					
					// Add a reference to the current Spot to the index
					spotIndex.add(j, temp2);
				}
				
				// Add a reference to the Spot to the index
				spotIndex.add(i, temp);
			}
			
			// If it is not a Spot, it is an ordinary spot
			else {
				temp.setNext(new Spot(null, Color.white, i));
				temp = temp.getNext();
				
				// Add a reference to the Spot in the spotIndex
				spotIndex.add(i, temp);
			}
		}
		
		// Initialize the starting spots (indexed 56-72)
		for(int i = 0, j = 55, k = 0; i < 4; i++)
			for(int k = 0; )
			
	
		// Finally, make the last Spot in the line reference the first
		temp.setNext(spotIndex.get(0));
		spotIndex.add(39, temp);
	}
	
	
	int counter = 57;
		for(int i = 0; i < 4; i++) {
			Player player = players.get(i);
			pieceIndex.put(player, new Spot[4]);
			
			for(int j = 0; j < 4; j++)
				pieceIndex.get(player)[i] = spotIndex.get(counter++);		
		}
 */
